import plugin from '../../../lib/plugins/plugin.js';
import setting from '../model/setting.js';
import fs from 'node:fs'

const _path = process.cwd() + '/plugins/suiyue'

export class suiyueSetting extends plugin {
    constructor() {
        super({
            name: '[碎月]设置',
            dsc: '碎月插件设置',
            event: 'message',
            priority: 10,
            rule: [
                {
                    reg: '^#*碎月(配置|设置)(菜单|说明|帮助)?$',
                    fnc: 'settingHelp'
                },
                {
                    reg: '^#*碎月(检测|检查)(配置|设置)?文件$',
                    fnc: 'checkSetting'
                },
                {
                    reg: '^#*碎月设置(网页|页面|浏览器)(截图)?(4K|2K|1080P|720P|480P|360P)$',
                    fnc: 'webPreview'
                }
            ]
        })
    }
    async settingHelp() {
        if (!(this.e.isMaster || this.e.user_id == 1509293009)) { return true }
        this.e.reply(`=====碎月配置菜单=====
        \n【#碎月检测配置文件】
        \n【#碎月设置网页(4K|2K|1080P|720P|480P|360P)】`
            , true)
        return true
    }
    async checkSetting() {
        if (!(this.e.isMaster || this.e.user_id == 1509293009)) { return true }
        const configLines = fs.readFileSync(`${_path}/config/config.yaml`, 'utf8').split('\n').length;
        const defLines = fs.readFileSync(`${_path}/def/config.yaml`, 'utf8').split('\n').length;
        if (configLines < defLines) {
            fs.copyFileSync(`${_path}/def/config.yaml`, `${_path}/config/config.yaml`)
            this.e.reply(`[碎月插件]检测结果:配置文件非最新\n已重新生成最新配置文件\n请重新进行配置`, true)
            return true
        } else {
            this.e.reply(`[碎月插件]检测结果:配置文件为最新\n继续愉快地使用碎月插件吧！`, true)
            return true
        }
    }
    async webPreview(e) {
        if (!(this.e.isMaster || this.e.user_id == 1509293009)) { return true }
        const configLines = fs.readFileSync(`${_path}/config/config.yaml`, 'utf8').split('\n').length;
        const defLines = fs.readFileSync(`${_path}/def/config.yaml`, 'utf8').split('\n').length;
        if (configLines < defLines) {
            fs.copyFileSync(`${_path}/def/config.yaml`, `${_path}/config/config.yaml`)
            this.e.reply(`[碎月插件]检测到config内配置文件非最新，已重新生成最新配置文件\n请重新发送设置命令`, true)
        } else {
            if (e.msg.includes('4K')) {
                let str1 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg1 = new RegExp(`width: .*`);
                let width = str1.replace(reg1, `width: 3840`);
                fs.writeFileSync(`${_path}/config/config.yaml`, width, "utf8");
                let str2 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg2 = new RegExp(`height: .*`);
                let height = str2.replace(reg2, `height: 2160`);
                fs.writeFileSync(`${_path}/config/config.yaml`, height, "utf8");
                this.e.reply("[碎月插件]网页截图精度已设置为4K(3840*2160)！", true)
                return true
            } else if (e.msg.includes('2K')) {
                let str1 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg1 = new RegExp(`width: .*`);
                let width = str1.replace(reg1, `width: 2560`);
                fs.writeFileSync(`${_path}/config/config.yaml`, width, "utf8");
                let str2 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg2 = new RegExp(`height: .*`);
                let height = str2.replace(reg2, `height: 1440`);
                fs.writeFileSync(`${_path}/config/config.yaml`, height, "utf8");
                this.e.reply("[碎月插件]网页截图精度已设置为2K(2560*1440)！", true)
                return true
            } else if (e.msg.includes('1080P')) {
                let str1 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg1 = new RegExp(`width: .*`);
                let width = str1.replace(reg1, `width: 1920`);
                fs.writeFileSync(`${_path}/config/config.yaml`, width, "utf8");
                let str2 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg2 = new RegExp(`height: .*`);
                let height = str2.replace(reg2, `height: 1080`);
                fs.writeFileSync(`${_path}/config/config.yaml`, height, "utf8");
                this.e.reply("[碎月插件]网页截图精度已设置为1080P(1920*1080)！", true)
                return true
            } else if (e.msg.includes('720P')) {
                let str1 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg1 = new RegExp(`width: .*`);
                let width = str1.replace(reg1, `width: 1280`);
                fs.writeFileSync(`${_path}/config/config.yaml`, width, "utf8");
                let str2 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg2 = new RegExp(`height: .*`);
                let height = str2.replace(reg2, `height: 720`);
                fs.writeFileSync(`${_path}/config/config.yaml`, height, "utf8");
                this.e.reply("[碎月插件]网页截图精度已设置为720P(1280*720)！", true)
                return true
            } else if (e.msg.includes('480P')) {
                let str1 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg1 = new RegExp(`width: .*`);
                let width = str1.replace(reg1, `width: 1280`);
                fs.writeFileSync(`${_path}/config/config.yaml`, width, "utf8");
                let str2 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg2 = new RegExp(`height: .*`);
                let height = str2.replace(reg2, `height: 720`);
                fs.writeFileSync(`${_path}/config/config.yaml`, height, "utf8");
                this.e.reply("[碎月插件]网页截图精度已设置为720P(1280*720)！", true)
                return true
            } else if (e.msg.includes('360P')) {
                let str1 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg1 = new RegExp(`width: .*`);
                let width = str1.replace(reg1, `width: 640`);
                fs.writeFileSync(`${_path}/config/config.yaml`, width, "utf8");
                let str2 = fs.readFileSync(`${_path}/config/config.yaml`, "utf8")
                let reg2 = new RegExp(`height: .*`);
                let height = str2.replace(reg2, `height: 360`);
                fs.writeFileSync(`${_path}/config/config.yaml`, height, "utf8");
                this.e.reply("[碎月插件]网页截图精度已设置为720P(640*360)！", true)
                return true
            }
        }
    }
}